package com.servlet;

import com.dao.CateGoryDao;
import com.dao.Impl.CateGoryDaoImp;
import com.entity.Category;
import com.entity.PageBean;
import com.util.MyUtil;
import com.util.TransBean;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 *
 * @author jie
 */
@WebServlet("/categoryController/*")
public class CateGoryServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String uri = request.getRequestURI();
        String method = uri.substring(uri.lastIndexOf("/") + 1);
        String target = null;
        switch (method) {
            case "add":
                target = add(request, response);
                break;
            case "show":
                show(request, response);
                break;
            case "modify":
                target = modify(request, response);
                break;
            case "findId":
                target = findId(request, response);
                break;
            case "delete":
                target = delete(request, response);
                break;
            case "findName":
                target = findName(request, response);
                break;
            case "findID":
                target = findID(request, response);
                break;
            default:break;
        }
        if (target != null) {
            request.getRequestDispatcher(target).forward(request, response);
        }
    }

    private String findID(HttpServletRequest request, HttpServletResponse response) {
        String name=request.getParameter("name");
        CateGoryDao dao=new CateGoryDaoImp();
        int id = dao.findId(name);
        MyUtil.jsonWriter(response,id);
        return null;
    }

    /**
     * 查找商品类型
     * @param request
     * @param response
     * @return
     */
    private String findName(HttpServletRequest request, HttpServletResponse response) {
        CateGoryDao dao=new CateGoryDaoImp();
        List<Map> name = dao.findName();
        MyUtil.jsonWriter(response,name);
        return null;
    }

    /**
     * 添加商品分类
     *
     * @param request
     * @param response
     * @return
     */
    private String add(HttpServletRequest request, HttpServletResponse response) {
        Category category = new Category();
        TransBean.populate(category, request.getParameterMap());
        CateGoryDao dao = new CateGoryDaoImp();
        dao.addCate(category);
        return "/categoryController/show";
    }

    /**
     * 查询全部商品类别
     *
     * @param request
     * @param response
     * @return
     */
    private void show(HttpServletRequest request, HttpServletResponse response) {
        PageBean pageBean = new PageBean();
        CateGoryDao dao = new CateGoryDaoImp();
        TransBean.populate(pageBean, request.getParameterMap());
        dao.findPage(pageBean);
        MyUtil.jsonWriter(response, pageBean);
    }

    /**
     * 修改数据
     *
     * @param request
     * @param response
     * @return
     */
    private String modify(HttpServletRequest request, HttpServletResponse response) {
        Category category = new Category();
        TransBean.populate(category, request.getParameterMap());
        CateGoryDao dao = new CateGoryDaoImp();
        int i = dao.modify(category);
        if (i != -1) {
            MyUtil.jsonWriter(response,category);
        }
        return null;
    }

    /**
     * 根据id查询
     *
     * @param request
     * @param response
     * @return
     */
    private String findId(HttpServletRequest request, HttpServletResponse response) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        CateGoryDao dao = new CateGoryDaoImp();
        Category cate = dao.findById(id);
        MyUtil.jsonWriter(response, cate);
        return null;
    }

    /**
     * 删除数据
     *
     * @param request
     * @param response
     * @return
     */
    private String delete(HttpServletRequest request, HttpServletResponse response) {
        CateGoryDao dao = new CateGoryDaoImp();
        Integer id = Integer.valueOf(request.getParameter("id"));
        dao.del(id);
        return "/categoryController/show";
    }
}
